/*
 * Copyright (C) 2021 Huawei Device Co., Ltd.
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *     http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
package it.carlom.stickyheader.example.adapter;

import it.carlom.stickyheader.ResourceTable;
import ohos.agp.components.*;
import ohos.app.Context;

import java.util.List;

/**
 * 列表 Adapter
 */
public class TextHideLineAdapter extends BaseItemProvider {

    private final List<String> titles;
    private final Context context;

    /**
     * 初始化
     *
     * @param context Context
     * @param titles  适配器数据源
     */
    public TextHideLineAdapter(Context context, List<String> titles) {
        this.context = context;
        this.titles = titles;
    }

    @Override
    public int getCount() {
        if (titles == null) {
            return 0;
        }
        return titles.size();
    }

    private Context getContext() {
        return context;
    }

    @Override
    public String getItem(int i) {
        return titles.get(i);
    }

    @Override
    public long getItemId(int index) {
        return index;
    }

    @Override
    public Component getComponent(int i, Component component, ComponentContainer componentContainer) {
        ListViewHolder listViewHolder;
        if (component == null) {
            component = LayoutScatter.getInstance(getContext()).parse(ResourceTable.Layout_item_main_no_line, componentContainer, false);
            listViewHolder = new ListViewHolder();
            listViewHolder.text = (Text) component.findComponentById(ResourceTable.Id_text1);
            component.setTag(listViewHolder);
        } else {
            listViewHolder = (ListViewHolder) component.getTag();
        }
        listViewHolder.text.setText(getItem(i));
        return component;
    }

    private static class ListViewHolder {
        Text text;
    }


}
